unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, Mask, IniFiles, ShlObj, _Informer,
  ShellAPI, ImgList;

type
  TMainForm = class(TForm)
    PageControl: TPageControl;
    AllJob: TTabSheet;
    WebLancer: TTabSheet;
    FreeLanceRu: TTabSheet;
    WeblancerList: TListBox;
    AllJobList: TListBox;
    FreeLanceRuList: TListBox;
    Settings: TTabSheet;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    MaskEdit1: TMaskEdit;
    Panel2: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Edit2: TEdit;
    MaskEdit2: TMaskEdit;
    Panel3: TPanel;
    Label5: TLabel;
    Label6: TLabel;
    Freq: TEdit;
    UpDown1: TUpDown;
    UpdateTimer: TTimer;
    Button1: TButton;
    ImageList: TImageList;
    Label7: TLabel;
    TransBar: TTrackBar;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
    procedure UpdateTimerTimer(Sender: TObject);
    procedure FreqKeyPress(Sender: TObject; var Key: Char);
    procedure FreqKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Button1Click(Sender: TObject);
    procedure WeblancerListDblClick(Sender: TObject);
    procedure WeblancerListDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure TransBarChange(Sender: TObject);
    procedure FreeLanceRuListDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure Edit1Exit(Sender: TObject);
    procedure MaskEdit1Exit(Sender: TObject);
    procedure Edit2Exit(Sender: TObject);
    procedure MaskEdit2Exit(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FreeLanceRuListDblClick(Sender: TObject);
  private
    { Private declarations }
    procedure LoadSet;
    procedure SaveSet;

    function  AppData: string;
    function  Lighter(Color:TColor; Percent:Byte):TColor;
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  Informer: TInformer;

implementation

{$R *.dfm}

{ TMainForm }

function TMainForm.Lighter(Color:TColor; Percent:Byte):TColor;
var
  r, g, b: Byte;
begin
  Color:=ColorToRGB(Color);
  r:=GetRValue(Color);
  g:=GetGValue(Color);
  b:=GetBValue(Color);
  r:=r+muldiv(255-r,Percent,100); 
  g:=g+muldiv(255-g,Percent,100);
  b:=b+muldiv(255-b,Percent,100);
  result:=RGB(r,g,b);
end;

function TMainForm.AppData: string;
var
  s: string;
begin
  SetLength(s, MAX_PATH);
  if not SHGetSpecialFolderPath(0, PChar(s), CSIDL_APPDATA, true) then
    s := '';
  Result := PChar(s) + '\';
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
  Informer.GetWeblancer;
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
  Informer.GetFreeLanceRu;
end;

procedure TMainForm.Edit1Exit(Sender: TObject);
begin
  Informer.WeblancerAcc.UserName:= Edit1.Text;
end;

procedure TMainForm.Edit2Exit(Sender: TObject);
begin
  Informer.FreeLanceRuAcc.UserName:= Edit2.Text;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  LoadSet;

  Informer:= TInformer.Create;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  SaveSet;
end;

procedure TMainForm.FreeLanceRuListDblClick(Sender: TObject);
var
  index: Integer;
begin
  index:= FreeLanceRuList.ItemIndex;
  ShellExecute(0, 'open', PAnsiChar(Informer.FreeLance[index].Link),
    nil, nil, 0);
end;

procedure TMainForm.FreeLanceRuListDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  x : Integer;
begin
  with FreeLanceRuList.Canvas do
  begin
    Brush.Color:= Lighter(clLime, 95);

    if Informer.FreeLance[Index].new then
      Brush.Color:= Lighter(clLime, 75);

    FillRect(Rect);

    ImageList.Draw(FreeLanceRuList.Canvas, Rect.Left+1, Rect.Top+7, 1, True);

    x:= Rect.Left + 20;

    if Informer.FreeLance[Index].Pro then
    begin
      Font.Color:= RGB(255, 107, 61);
      Font.Size:= 11;
      TextOut(x ,Rect.Top + 5, 'PRO');
      Inc(x, 30);
      Font.Color:= clBlack;
      Font.Size:= 8;
    end;

    if odSelected in State then
      Font.Color:= clGreen;

    TextOut(x, Rect.Top+2, Informer.FreeLance[Index].Name);
    TextOut(x, Rect.Top+15, Informer.FreeLance[Index].Time);
  end;
end;

procedure TMainForm.FreqKeyPress(Sender: TObject; var Key: Char);
begin
  if Ord(Key) = VK_BACK then
    Exit;  
  if not (Ord(Key) in [48..57]) then
    Key:= #0;
end;

procedure TMainForm.FreqKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if StrToInt(Freq.Text) > 60 then
    Freq.Text:= '60';
  if StrToInt(Freq.Text) < 1 then
    Freq.Text:= '1'; 
end;

procedure TMainForm.LoadSet;
var
  ini: TIniFile;
begin
  if not DirectoryExists(AppData + 'Job Informer\') then
    CreateDir(AppData + 'Job Informer\');

  ini:= TIniFile.Create(AppData + 'Job Informer\Settins.ini');
    MainForm.Left:=   ini.ReadInteger('Window', 'Left', 640);
    MainForm.Top:=    ini.ReadInteger('Window', 'Top', 480);
    MainForm.Width:=  ini.ReadInteger('Window', 'Width', 352);
    MainForm.Height:= ini.ReadInteger('Window', 'Height', 255);

    TransBar.Position:= ini.ReadInteger('Window', 'Transparent', 255);

    Freq.Text:= IntToStr(ini.ReadInteger('Update', 'Freq', 30));
    UpdateTimer.Interval:= (ini.ReadInteger('Update', 'Freq', 30) * 1000 * 60);

    Edit1.Text:= ini.ReadString('WebLancer', 'User', '');
    MaskEdit1.Text:= ini.ReadString('WebLancer', 'Pass', '');

    Edit2.Text:= ini.ReadString('FreeLanceRu', 'User', '');
    MaskEdit2.Text:= ini.ReadString('FreeLanceRu', 'Pass', '');
  ini.Free;
end;

procedure TMainForm.MaskEdit1Exit(Sender: TObject);
begin
  Informer.WeblancerAcc.PassWord:= MaskEdit1.Text;
end;

procedure TMainForm.MaskEdit2Exit(Sender: TObject);
begin
  Informer.FreeLanceRuAcc.PassWord:= MaskEdit2.Text;
end;

procedure TMainForm.SaveSet;
var
  ini: TIniFile;
begin
  ini:= TIniFile.Create(AppData + 'Job Informer\Settins.ini');
    ini.WriteInteger('Window', 'Left', MainForm.Left);
    ini.WriteInteger('Window', 'Top', MainForm.Top);
    ini.WriteInteger('Window', 'Width', MainForm.Width);
    ini.WriteInteger('Window', 'Height', MainForm.Height);

    ini.WriteInteger('Window', 'Transparent', MainForm.AlphaBlendValue);

    ini.WriteInteger('Update', 'Freq', StrToInt(Freq.Text));

    ini.WriteString('WebLancer', 'User', Edit1.Text);
    ini.WriteString('WebLancer', 'Pass', MaskEdit1.Text);

    ini.WriteString('FreeLanceRu', 'User', Edit2.Text);
    ini.WriteString('FreeLanceRu', 'Pass', MaskEdit2.Text);
  ini.Free;
end;

procedure TMainForm.TransBarChange(Sender: TObject);
begin
  MainForm.AlphaBlendValue:= TransBar.Position;
end;

procedure TMainForm.UpdateTimerTimer(Sender: TObject);
begin
  ShowMessage('Updating...');
end;

procedure TMainForm.UpDown1Click(Sender: TObject; Button: TUDBtnType);
begin
  UpdateTimer.Interval:= (StrToInt(Freq.Text) * 1000 * 60);
end;

procedure TMainForm.WeblancerListDblClick(Sender: TObject);
var
  index: Integer;
begin
  index:= WeblancerList.ItemIndex;
  ShellExecute(0, 'open', PAnsiChar(Informer.WebLancer[index].Link),
    nil, nil, 0);
end;

procedure TMainForm.WeblancerListDrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  with WeblancerList.Canvas do
  begin
    Brush.Color:= Lighter(clSkyBlue, 85);

    if Informer.WebLancer[Index].new then
      Brush.Color:= Lighter(clSkyBlue, 65);;

    FillRect(Rect);

    ImageList.Draw(WebLancerList.Canvas, Rect.Left+1, Rect.Top+7, 0, True);

    if odSelected in State then
        Font.Color:=clBlue;

//    Font.Size:= 11;
    TextOut(Rect.Left+20,Rect.Top+2, Informer.WebLancer[Index].name);
//    Font.Size:= 8;
    TextOut(Rect.Left+20,Rect.Top+15, Informer.WebLancer[Index].time);
  end;
end;

end.
